package com.penjin.android.activity.shenpi;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.text.Html;
import android.view.View;
import android.widget.TextView;

import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;
import com.penjin.android.R;
import com.penjin.android.constants.HttpConstants;
import com.penjin.android.domain.PenjinCompany;
import com.penjin.android.domain.PenjinStaff;
import com.penjin.android.domain.PenjinUser;
import com.penjin.android.domain.PenjinWorkflowForm;
import com.penjin.android.http.HttpConstant;
import com.penjin.android.http.HttpService;
import com.penjin.android.service.UserService;
import com.penjin.android.utils.BitmapUtils;
import com.penjin.android.utils.HttpUtil;
import com.penjin.android.utils.ToastHelper;
import com.penjin.android.view.CircleImageView;
import com.penjin.android.view.CustomProgressDialog;
import com.penjin.android.view.TitleBarView;
import com.penjin.android.widget.CustomEditDialog;

import org.apache.http.Header;

/**
 * Created by Administrator on 2016/4/13.
 */
public class MyShenpiDetailActivity extends FragmentActivity implements View.OnClickListener {

    PenjinUser user;
    PenjinCompany company;
    PenjinStaff staff;
    PenjinWorkflowForm bill = null;

    HttpService httpService;
    TextHttpResponseHandler handler;
    TextHttpResponseHandler replyHandler;

    TitleBarView titleBarView;
    TextView billNumber;
    TextView billName;
    TextView staffName;
    CircleImageView staffAvatar;
    TextView info;

    View ok;
    View cancle;
    CustomProgressDialog progressDialog;

    CustomEditDialog confirmDialog;
    int confirmType = -1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_activity_shenpi_detail);
        initData(savedInstanceState);
        initView();
        initHttp();
    }

    private void initHttp() {
        httpService = HttpService.getInstance(this.getApplicationContext());
        handler = new TextHttpResponseHandler() {
            @Override
            public void onFailure(int i, Header[] headers, String s, Throwable throwable) {

            }

            @Override
            public void onSuccess(int i, Header[] headers, String s) {

            }
        };

        replyHandler = new TextHttpResponseHandler() {
            @Override
            public void onFailure(int i, Header[] headers, String s, Throwable throwable) {
                progressDialog.dismiss();
                ToastHelper.showServerError(getApplicationContext());
                if (s != null) {
                    System.out.println(s);
                }
            }

            @Override
            public void onSuccess(int i, Header[] headers, String s) {
                progressDialog.dismiss();
                confirmDialog.dismiss();
                ToastHelper.showMessage(s,getApplicationContext());
                System.out.println(s);
                MyShenpiDetailActivity.this.finish();
            }
        };
    }

    private void initDialog() {
        progressDialog = CustomProgressDialog.createDialog(this);
        progressDialog.setMessage("请稍后...");

        confirmDialog = new CustomEditDialog(this, R.style.PenjinCommonDialog, "审批意见", null);
        confirmDialog.setCustomEditDialogListnerlistner(new CustomEditDialog.CustomEditDialogListner() {
            @Override
            public void ok(String msg, View view) {
                if (msg != null && !msg.equals("")) {
                    RequestParams requestParams = new RequestParams();
                    requestParams.put("userName", user.getPhone());
                    requestParams.put("companyId", company.getCompanyId());
                    requestParams.put("remark", msg);
                    requestParams.put("Id", bill.auditId);
                    requestParams.put("Flag", confirmType + "");
                    progressDialog.show();
                    try {
                        httpService.postRequest(MyShenpiDetailActivity.this, HttpConstants.HOST + HttpConstants.Audit, requestParams, replyHandler);
                    } catch (Exception e) {
                        progressDialog.dismiss();
                        ToastHelper.showConnectionError(getApplicationContext());
                    }
                }
            }

            @Override
            public void cancel(String msg) {
                confirmDialog.dismiss();
            }
        });
    }

    private void initView() {
        initDialog();
        ok = findViewById(R.id.ok);
        cancle = findViewById(R.id.cancel);
        ok.setOnClickListener(this);
        cancle.setOnClickListener(this);

        titleBarView = (TitleBarView) findViewById(R.id.titleBarView);
        titleBarView.setTitleBarListener(new TitleBarView.TitleBarListener() {
            @Override
            public void left(View view) {
                finish();
            }

            @Override
            public void center(View view) {

            }

            @Override
            public void right(View view) {

            }
        });
        billNumber = (TextView) findViewById(R.id.billNumber);
        billNumber.setText(bill.billNumber);
        billName = (TextView) findViewById(R.id.billTypeName);
        billName.setText(bill.billName);

        staffName = (TextView) findViewById(R.id.name);
        staffName.setText(bill.staffName);

        staffAvatar = (CircleImageView) findViewById(R.id.avatar);
        BitmapUtils.loadUrlImage(bill.stafffAvatar, staffAvatar, this, true);

        info = (TextView) findViewById(R.id.info);
        String htmlInfo = parseInfo(bill.info);
        info.setText(Html.fromHtml(htmlInfo));

    }

    private void initData(Bundle savedInstanceState) {
        UserService userService = UserService.getInstance(this.getApplicationContext());
        user = userService.getCurrentUser();
        if (user.isInCompany()) {
            company = userService.getCurrentCompany();
            staff = userService.getCurrentStaff();
            Intent intent = getIntent();
            if (intent != null) {
                try {
                    bill = intent.getParcelableExtra("bill");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (bill == null) {
                bill = savedInstanceState.getParcelable("bill");
            }
        } else {
            ToastHelper.showNotBindCompany(getApplicationContext());
            finish();
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putParcelable("bill", bill);
    }

    private String parseInfo(String info) {
        String[] strs = info.split(";");
        StringBuilder stringBuilder = new StringBuilder();
        for (String str : strs) {
            stringBuilder.append("<p>" + str + "</p>");
        }
        return stringBuilder.toString();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ok:
                if (HttpUtil.isNetworkAvailable(this)) {
                    confirmType = 1;
                    confirmDialog.setHint("请填写审批意见");
                    confirmDialog.show();
                } else {
                    ToastHelper.showNoNetwork(getApplicationContext());
                }
                break;
            case R.id.cancel:
                if (HttpUtil.isNetworkAvailable(this)) {
                    confirmDialog.setHint("请填写审批意见");
                    confirmDialog.show();
                    confirmType = 0;
                } else {
                    ToastHelper.showNoNetwork(getApplicationContext());
                }
                break;
            default:
                break;
        }
    }
}
